package com.md.mainpage.presenter

import com.md.basedpc.base.BasePresenter
import com.md.mainpage.interfaces.AlbumContract
import com.md.mainpage.model.AlbumModel
import com.md.network.api.Album

class AlbumPresenter : BasePresenter<AlbumContract.View>(), AlbumContract.Presenter {

    private val model: AlbumModel by lazy {
        AlbumModel()
    }

    override fun getAlbum(albumId: Int) {
        checkViewAttached()
        val disposable = model.getAlbumInfo(albumId).subscribe({ albumInfo ->
            mRootView?.apply {
                wrapAudio(albumInfo)
                showAlbumInfo(albumInfo)

            }
        }, { throwable ->
            mRootView?.apply {
                showError(throwable.toString())
            }
        })

        disposable?.let { addSubscription(it) }
    }

    private fun wrapAudio(album: Album?) {
        if (album != null && album?.audios.isNotEmpty()) {
            for (ad in album.audios) {
                ad.albumId = album.id
                ad.albumAuthor = album.author ?: "未知作者"
                ad.albumName = album.name
                ad.albumDes = album.desc
            }
        }
    }


}